import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.*;
import java.io.*;

/**
 * Display class Responsibilities: - Displays user interface as it is: Screen
 * design & sample session: UserName1: 4 pt. UserName2: 19pt. // Header - points
 * . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 .10 .11 .12 .13 .14 .15 . . . . . . . . .
 * . . . . . . . .
 * ==-------------------------------------------------------------== // a | | |
 * | | | | | | C | | | | | | | a //
 * ==-------------------------------------------------------------== // b | | |
 * | | | | | | O | | | | | | | b //
 * ==-------------------------------------------------------------== // c | | |
 * | | | | | | M | | | | | | | c //
 * ==-------------------------------------------------------------== // d | | |
 * | | | | | | P | | | | | | | d //
 * ==-------------------------------------------------------------== // e | | |
 * | | | | | | U | | | | | | | e //
 * ==-------------------------------------------------------------== // f | | |
 * | | | | | | T | | | | | | | f //
 * ==-------------------------------------------------------------== // g | | |
 * | | | | | | E | | | | | | | g //
 * ==-------------------------------------------------------------== // h | | |
 * | | | | X | O | R | | | | | | | h //
 * ==-------------------------------------------------------------== // i | | |
 * | | | | | | S | | | | | | | i // Board display
 * ==-------------------------------------------------------------== // j | | |
 * | | | | | | C | | | | | | | j //
 * ==-------------------------------------------------------------== // k | | |
 * | | | | | | I | | | | | | | k //
 * ==-------------------------------------------------------------== // l | | |
 * | | | | | | E | | | | | | | l //
 * ==-------------------------------------------------------------== // m | | |
 * | | | | | | N | | | | | | | m //
 * ==-------------------------------------------------------------== // n | | |
 * | | | | | | C | | | | | | | n //
 * ==-------------------------------------------------------------== // o | | |
 * | | | | | | E | | | | | | | o //
 * ==-------------------------------------------------------------== // . . . .
 * . . . . . . . . . . . . // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //
 * 
 *Tiles on hand:[ A,A,J,K,L,M,P,W,X,Z, ,$,#,# ] //
 * --------------------------------------------------------------------- //
 * Player UserName1 **> h7 r -> "xor" <- ACCEPTED ( 4 pt. ) // Player UserName2
 * ---> a9 d -> "computerscience" <- ACCEPTED ( 19 pt. ) // Notification area
 * Player UserName1 **> n12 l -> "slice" <- NOT ACCEPTED ( 0 pt. ) //
 * --------------------------------------------------------------------- //
 * UserName2 ?: i9 r simplex // User Interaction area
 * 
 * @author kkopiec
 * 
 */
// TODO: Manage it static !!!
public class Display {
	static protected ArrayList<String> board = new ArrayList<String>();
	static protected Map<Integer, Integer> colIndexes;
	static protected Map<Integer, Integer> rowIndexes;

	protected static MessageQueue notifArea = new MessageQueue(5);

	public Display() {
		colIndexes = new HashMap<Integer, Integer>();
		rowIndexes = new HashMap<Integer, Integer>();
		boardSetupInit();
	}

	/**
	 * Reads Board template from file "board.template" Fills out array of
	 * strings representing display form of the game board Sets up indexes of
	 * rows and cols in order to display board data structure
	 */
	protected static void boardSetupInit() {
		try {
			File boardTemplate = new File("board.template");
			FileReader frd = new FileReader(boardTemplate);
			BufferedReader brd = new BufferedReader(frd);
			String line = null;
			int numberCodeOffset = 0;
			int lineno = 0;
			while ((line = brd.readLine()) != null) {
				Integer idx = new Integer(0);
				Integer letterCodeOffset = 0;
				boolean lineIndexed = false;
				CharacterIterator it = new StringCharacterIterator(line);
				for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it
						.next()) {
					if (ch == '#') {
						colIndexes.put(new Integer(letterCodeOffset), idx);
						letterCodeOffset++;
						lineIndexed = true;

					}
					idx++;

				}
				if (lineIndexed) {
					rowIndexes.put(new Integer(numberCodeOffset), new Integer(
							lineno));
					numberCodeOffset++;
				}
				lineno++;
				line = line.replace('#', ' ');
				board.add(line);
			}
			brd.close();
		} catch (IOException ex) {
			System.out
					.println("Something went terribly wrong while loading board template !!!");
			ex.printStackTrace();
		}
	}

	/**
	 * Display Board element of the screen
	 */
	protected static void synchronizeBoard() {
		for (int y = 0; y < Board.getBoardSize(); y++) {
			for (int x = 0; x < Board.getBoardSize(); x++) {
				Field field = Board.pBoard[y][x];
				
					String dspL = board.get(rowIndexes.get(new Integer(y)));
					char[] carr = new char[dspL.length()];
					carr = dspL.toCharArray();
					carr[colIndexes.get(new Integer(x)) - 1] = field.toString()
							.charAt(0);
					carr[colIndexes.get(new Integer(x))] = field.toString()
							.charAt(1);
					carr[colIndexes.get(new Integer(x)) + 1] = field.toString()
							.charAt(2);
					int idx = board.indexOf(dspL);
					board.remove(idx);
					board.add(idx, new String(carr));
				
			}
		}
	}

	protected static void displayBoard() {
		synchronizeBoard();
		for (String line : board) {
			System.out.println(line);
		}
		//System.out.println(colIndexes);
		//System.out.println(rowIndexes);
	}

	/**
	 * Display header : players names and their scores
	 */
	protected static void displayHeader() {
		System.out.println(Game.getScores());
	}

	/**
	 * Displays Screen
	 */
	public static void displayScreen() {
		displayHeader();
		displayBoard();
		displayNotificationArea();
		displayPromptArea();
	}

	protected static void displayNotificationArea() {
		System.out.println("-----------------------------------------------------------------------");
		for (String each : notifArea.notifications()) {
			System.out.println(each);
		}
		System.out.println("-----------------------------------------------------------------------");
	}

	public static void displayNotification(String n) {
		notifArea.enqueue(n);
	}

	protected static void displayPromptArea() {
		System.out.print("Nerdle -> ");
	}

	public static String io(String prompt) {
		System.out.print(prompt);
		String input;
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			input = in.readLine().toUpperCase();
		} catch (IOException e) {
			return "-------";
		}
		return input;
	}
	public static void refresh(){
		//System.out.println("\f");
		displayScreen();
	}
	

}
